import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class FlutterH5AssetJump extends StatefulWidget {
  const FlutterH5AssetJump({super.key});

  @override
  State<FlutterH5AssetJump> createState() => _FlutterH5AssetJumpState();
}

class _FlutterH5AssetJumpState extends State<FlutterH5AssetJump> {
  late WebViewController controller;

  get _loadBtn => FilledButton(
    onPressed: () {
      _onLoadFlutterAssets(context);
    },
    child: Text('加载H5', style: TextStyle(color: Colors.white)),
  );

  @override
  void initState() {
    super.initState();

    controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('通过loadFlutterAssets的方式加载H5'),
        actions: [_loadBtn],
      ),
      body: WebViewWidget(controller: controller),
    );
  }

  void _onLoadFlutterAssets(BuildContext context) async {
    await controller.loadFlutterAsset('assets/hiH5/index.html');
  }
}
